package com.sohoometer.station.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.nari.coreframe.dao.hibernate3.GenericDaoImpl;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResultWithPageCond;
import com.sohoometer.station.bean.PowerMeterData;
import com.sohoometer.station.bean.WaterMeterData;
import com.sohoometer.station.bean.WaterMeterInfo;

@Repository("waterMeterDataDao")
public class WaterMeterDataDaoImpl extends GenericDaoImpl<WaterMeterData, String> implements WaterMeterDataDao {
	@Autowired
	public WaterMeterDataDaoImpl(SessionFactory sessionFactory) {
		setSessionFactory(sessionFactory);
	}

	@Override
	public ResultWithPageCond<WaterMeterData> queryWaterMeterDatas(String waterMeterNo, List<WaterMeterInfo> meters,Date startQueryTime, Date endQueryTime, PageCond pageCond) {
		if(meters==null) meters = new ArrayList<WaterMeterInfo>();
		List<String> meterNos = convertToMeterNo(meters);
		if (StringUtils.isNotBlank(waterMeterNo)) {
			meterNos.add(waterMeterNo);
		}
		if(meterNos.size()<1) return null;
	Session currentSession = getCurrentSession();
	Criteria criteria = currentSession.createCriteria(WaterMeterData.class);
	Criteria countCriteria = currentSession.createCriteria(WaterMeterData.class);
//	DetachedCriteria criteria = createDetachedCriteria().add(Restrictions.in("powerMeterNo", list));
	//查询数量
	countCriteria.add(Restrictions.in("waterMeterNo", meterNos));
	  criteria.add(Restrictions.in("waterMeterNo", meterNos));
	  if(startQueryTime!=null&&endQueryTime!=null) {
		  countCriteria.add(Restrictions.between("uploadTime", startQueryTime, endQueryTime));
	  criteria.add(Restrictions.between("uploadTime", startQueryTime, endQueryTime));
	  }
	  criteria.setFirstResult(pageCond.getStart());
	  criteria.setMaxResults(pageCond.getLimit());
	  criteria.addOrder(Order.desc("uploadTime"));
	  countCriteria.setProjection(Projections.rowCount());//投影查询 查询数目
	  Object uniqueResult = countCriteria.uniqueResult();
	  Long i = (Long) uniqueResult;
	  ResultWithPageCond<WaterMeterData> result = new ResultWithPageCond<WaterMeterData>();
	  pageCond.setTotal(i.intValue());
	  //设置数目
	  result.setPageCond(pageCond);
	  result.setList(criteria.list());
	  return result;
	}
	
	private List<String> convertToMeterNo(List<WaterMeterInfo> meters) {
		List<String>  result = new ArrayList<String>();
		for(WaterMeterInfo info : meters) {
			if(StringUtils.isBlank(info.getWaterMeterNo()))  continue;
			result.add(info.getWaterMeterNo());
		}
		return result;
	}
	
}
